#include"MyPlayer.h"
typedef struct _LINKINFO{
DATAINFO*head;
DATAINFO*end;
int datasize;
uint32_t gettimestamp;
}LINKINFO;
static LINKINFO linkvideoinfo={NULL,NULL,0,0};
static LINKINFO linkaudioinfo={NULL,NULL,0,0};
static SDL_mutex*mut=NULL;
void InitSDLMutx(){
     mut=SDL_CreateMutex();
}
void UnInitSDLMutx(){
     SDL_DestroyMutex(mut);
     mut=NULL;
}

int IsVideoFastThenAudio(){
    int bFast=0;
    SDL_mutexP(mut);
    if(linkaudioinfo.gettimestamp>0){
       if(linkvideoinfo.gettimestamp>linkaudioinfo.gettimestamp){
          bFast=1;
       }else
       if((linkvideoinfo.gettimestamp+100)<linkaudioinfo.gettimestamp){
           bFast=-1;
       }    
    }
    SDL_mutexV(mut);
    return bFast;
}
int  GetPacketLength(LINKINFO*info){
     int length=0;
     SDL_mutexP(mut);
     length=info->datasize;
     SDL_mutexV(mut);
     return length;
}
void PushPacketToList(DATAINFO*data,LINKINFO*info){
     SDL_mutexP(mut);
     if(info->head==NULL){
        info->head=info->end=data;
     }else{
       info->end->Next=data;
       info->end=info->end->Next;
     }
     info->datasize++;
     SDL_mutexV(mut);
}
DATAINFO*GetPacketFromList(LINKINFO*info){
  DATAINFO*pos=NULL;
  SDL_mutexP(mut);
  if(info->head!=NULL){
     pos= info->head;
     if(info->head==info->end){
        info->head=info->end=NULL;
     }else{
        info->head=info->head->Next;
     }
     info->gettimestamp=pos->timestamp;
     info->datasize--;
  }
  SDL_mutexV(mut);
  return pos;
}
int  GetVideoPacketLength(){
     return GetPacketLength(&linkvideoinfo);
}
DATAINFO*GetVideoPacket(){
    return GetPacketFromList(&linkvideoinfo);
}
void PushVideoPacket(uint8_t *buffer,int width,int height,uint32_t timestamp){
    DATAINFO*data=(DATAINFO*)malloc(sizeof(DATAINFO));
    memset(data,0,sizeof(DATAINFO));
    data->data=buffer;
    data->width=width;
    data->height=height;
	data->timestamp=timestamp;
    data->Next=NULL;
    PushPacketToList(data,&linkvideoinfo);
}
int  GetAudioPacketLength(){
     return GetPacketLength(&linkaudioinfo);
}
void PushAudioPacket(uint8_t *buffer,int length,uint32_t timestamp){
    DATAINFO*data=(DATAINFO*)malloc(sizeof(DATAINFO));
    memset(data,0,sizeof(DATAINFO));
    data->data=buffer;
    data->length=length;
	data->timestamp=timestamp;
    data->Next=NULL;
    PushPacketToList(data,&linkaudioinfo);
}

DATAINFO*GetAudioPacket(){
    return GetPacketFromList(&linkaudioinfo);
}
